package com.xr.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.sunnyday.dao.OrderSettingDao;
import com.xr.sunnyday.exception.AssociationException;
import com.xr.sunnyday.pojo.OrderSetting;
import com.xr.sunnyday.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    @Transactional
    public void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            OrderSetting osInDB= orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
            if(null!=osInDB){
                if(osInDB.getReservations()>orderSetting.getNumber()){
                    //如果excel表中可预约人数<已预约人数，则报错
                    throw new AssociationException("可预约人数不能小于已预约人数");
                }else{
                    orderSettingDao.updateNumber(orderSetting);
                }
            }else{
                orderSettingDao.add(orderSetting);
            }
        }
    }

    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String month) {
        String startDate = month + "-01";
        return orderSettingDao.getOrderSettingByMonth(startDate);
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        OrderSetting osInDB= orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        if(null!=osInDB){
            if(osInDB.getReservations()>orderSetting.getNumber()){
                //如果excel表中可预约人数<已预约人数，则报错
                throw new AssociationException("可预约人数不能小于已预约人数");
            }else{
                orderSettingDao.updateNumber(orderSetting);
            }
        }else{
            orderSettingDao.add(orderSetting);
        }
    }
}
